//保险计算脚本
var Calc = {
    configUrl:'',
    createNew:function(){
        var calc = {};
        
        calc.dsz = 0;//第三者责任险
        calc.clss = 0;//车辆损失险
        calc.dqx = 0;//盗抢险
        calc.bl = 0; //玻璃损失险
        calc.sjx = 0; //司机座位责任险
        calc.ckx = 0; //乘客座位责任险
        calc.hh = 0;//车身划痕险
        calc.zrx = 0;//自燃险
        calc.ssx = 0;//涉水险
        calc.bjmp = 0;//不计免赔
        calc.jqx =0;//交强险
        calc.ccs = 0;//车船税
        
        calc.b_dsz = true;//第三者责任险
        calc.b_clss = true;//车辆损失险
        calc.b_dqx = true;//盗抢险
        calc.b_bl = true; //玻璃损失险
        calc.b_sjx = true; //司机座位责任险
        calc.b_ckx = true; //乘客座位责任险
        calc.b_hh = true;//车身划痕险
        calc.b_zrx = true;//自燃险
        calc.b_ssx = true;//涉水险
        calc.b_bjmp = true;//不计免赔
        
        calc.price = 0;
        calc.seatnum = 5;
        calc.pl = 1600;
        calc.year = 1;
        calc.dszM = 100000;//第三者保险额10万
        calc.blType = 0; //玻璃类型：国产
        calc.sjM = 10000; //司机保额：1万
        calc.ckM = 10000; //乘客座位保额:1万
        calc.hhM = 2000; //划痕险保额：2千
        
        calc.cb = null;
        
        calc.setBuyTime = function(buytime){
            var date1=buytime;
            var now=new Date();
            var date2=now.getFullYear()+"-"+(now.getMonth()+1)+"-"+now.getDate();
            date1=date1.split("-");
            date1=parseInt(date1[0])*12+parseInt(date1[1]);
            date2=date2.split("-");
            date2=parseInt(date2[0])*12+parseInt(date2[1]);
            var m=Math.abs(date1-date2);
            this.year= Math.round(m / 12) + ((m % 12)>0?1:0);
        }

        calc.getConfig = function(cb,company){
            $.post(this.configUrl,"company="+company,function(data){
                if(data.status){
                    calc.config = data.info;
                    calc.calcAll(true);
                }
            },'json');
        };
        calc.setParams = function(price,seatnum,year,pl){
            this.price = price;
            this.seatnum = (seatnum<=0)?5:seatnum;
            this.year = year;
            this.pl = pl;
        };
        
        calc.getIndex = function(){
            var n;
            if(this.seatnum<6){
                n = 0;
            }else if(this.seatnum<=10){
                n = 1;
            }else{
                n = 2;
            }
            return n;
        };
        //计算第三者责任险
        calc.getDsz = function(){
            var n = this.getIndex(),nn=0;
            switch(this.dszM){
            case 50000:nn=0;break;
            case 100000:nn=1;break;
            case 150000:nn=2;break;
            case 200000:nn=3;break;
            case 300000:nn=4;break;
            case 500000:nn=5;break;
            case 1000000:nn=6;break;
            }
            this.dsz = parseInt(this.config.dsz[n][nn]);
        };
        
        //车辆损失险
        calc.getClss = function(){
            var n = this.getIndex(),nn,y=this.year,fl;
            if(y<1) nn=0;
            else if(y<=2) nn=1;
            else if(y<=6) nn=2;
            else nn = 3;
            fl = this.config.clss[n][nn];
            this.clss = Math.round(parseInt(fl[0])+parseFloat(this.price*fl[1]/100));
        };
        
        //盗抢险
        calc.getDqx = function(){
            var n = this.getIndex(),fl=this.config.dqx[n];
            this.dqx = Math.round(parseFloat(fl[0])+this.price*parseFloat(fl[1])/100);
        };
        
        //玻璃单独破碎险
        calc.getBl = function(){
            var n = this.getIndex();
            this.bl = Math.round(this.price*parseFloat(this.config.bl[n][this.blType])/100);
        };
        
        //车上人员责任险
        calc.getCsry = function(){
            var n = this.getIndex(),fl=this.config.csry[n];
            this.sjx = Math.round(this.sjM*parseFloat(fl[0])/100);
            this.ckx = Math.round(this.ckM*parseFloat(fl[1])/100*(this.seatnum-1));
        };
        
        //车身划痕险
        calc.getHh = function(){
            var n=0,nn=0,nnn;
            switch(this.hhM){
            case 2000:n=0;break;
            case 5000:n=1;break;
            case 10000:n=2;break;
            case 20000:n=3;break;
            }
            if(this.price<300000){
                nnn=0;
            }else if(this.price<500000){
                nnn=1;
            }else{
                nnn=2;
            }
            if(this.year>=2) nn=1;
            this.hh=parseInt(this.config.cshh[n][nn][nnn]);
        };
        
        //自燃险
        calc.getZrx = function(){
            this.zrx = Math.round(this.price*parseFloat(this.config.zrx)/100);
        };
        
        //涉水险
        calc.getSsx = function(){
            this.ssx = Math.round(this.price*parseFloat(this.config.ssx)/100);
        }
        
        //不计免赔损失险
        calc.getBjmp = function(){
            var fl = this.config.bjmp;
            this.bjmp = Math.round(
                        (this.b_dsz?fl[0]*this.dsz/100:0)+
                        (this.b_clss?fl[1]*this.clss/100:0)+
                        (this.b_sjx?fl[2]*this.sjx/100:0)+
                        (this.b_ckx?fl[2]*this.ckx/100:0)+
                        (this.b_dqx?fl[3]*this.dqx/100:0)+
                        (this.b_zrx?fl[4]*this.zrx/100:0)+
                        (this.b_hh?fl[6]*this.hh/100:0)+
                        (this.b_ssx?fl[9]*this.ssx/100:0));
        }
        
        //交强险
        calc.getJqx = function(){
            if(this.seatnum<6){
                this.jqx = parseInt(this.config.jqx[0][0]);
            }else{
                this.jqx = parseInt(this.config.jqx[1][0]);
            }
        }
        
        calc.getCcs = function(){
            var n = 1;
            if(this.pl<1000) n=0;
            else if(this.pl<=1600) n=1;
            else if(this.pl<=2000) n=2;
            else if(this.pl<=2500) n=3;
            else n=4;
            this.ccs = parseInt(this.config.ccs[n]);
        }
        
        calc.calcAll = function(bInit){
            this.getDsz();
            this.getClss();
            this.getDqx();
            this.getBl();
            this.getCsry();
            this.getHh();
            this.getZrx();
            this.getSsx();
            this.getBjmp();
            this.getJqx();
            this.getCcs();
            this.getTotal();
            if(this.cb) this.cb(bInit);
        }
        
        calc.getTotal = function(){
            this.getBjmp();
            return Math.round(
                        (this.b_dsz?this.dsz:0)+
                        (this.b_clss?this.clss:0)+
                        (this.b_sjx?this.sjx:0)+
                        (this.b_ckx?this.ckx:0)+
                        (this.b_dqx?this.dqx:0)+
                        (this.b_zrx?this.zrx:0)+
                        (this.b_hh?this.hh:0)+
                        (this.b_bl?this.bl:0)+
                        (this.b_ssx?this.ssx:0) + 
                        (this.b_bjmp?this.bjmp:0));    
        }
        calc.getQz = function(){
            return this.jqx+this.ccs;
        }
        
        return calc;
    }  
};
